#pragma once
#include <iostream>
#include <string>
#include <cstdarg>
#include <unistd.h>
#define LOG_NORMAL "log.txt"

#define LOG_ERR "log.error"


#define DEBUG 0
#define NORMAL 1
#define WARNING 2
#define ERROR 3
#define FATAL 4
//日志功能
const char* to_levelstr(int level)
{
    switch(level)
    {
        case DEBUG: return "DEBUG";
        case NORMAL: return "NORMAL";
        case WARNING: return "WARNING";
        case ERROR: return "ERROR";
        case FATAL: return "FATAL";
        default: return nullptr;
    }
}
void LogMessage(int level,const char* format,...)
{
    //[日志等级][时间戳/时间][pid][message]
#define NUM 1024
    char logprefix[NUM];
    snprintf(logprefix,sizeof(logprefix),"[%s][%ld][pid:%d]",to_levelstr(level),(long int)time(nullptr),getpid());//日志前缀

    char logcontent[NUM];
    va_list arg;
    va_start(arg,format);
    vsnprintf(logcontent,sizeof(logcontent),format,arg);
    //std::cout<<logprefix<<logcontent<<std::endl;
    FILE* log=fopen(LOG_NORMAL,"a");
    FILE* err=fopen(LOG_ERR,"a");
    if(log!=nullptr&&err!=nullptr)
    {
        FILE* curr=nullptr;
        if(level==DEBUG||level==NORMAL||level==WARNING)
        {
            curr=log;
        }
        else if(level==ERROR||level==FATAL)
        {
            curr=err;
        }
        if(curr!=nullptr)
        {
            fprintf(curr,"%s%s\n",logprefix,logcontent);
        }
        fclose(log);
        fclose(err);
    }
}